<?php

namespace app\admin\controller\customer;

use app\admin\controller\Base;
use think\Db;
use app\home\controller\Wxpay;
use think\Validate;

/**
 * 用户组管理
 * Class GroupController
 * @package app\admin\controller\auth
 */
class Product extends Base
{

    public function index()
    {
        return view();
    }

    /*管理产品*/
    public function addproduct()
    {
        $post = $this->request->param();
        if ($this->request->isPost()) {
            $validate = Validate::make([
                's_time|开始时间' => 'require|date',
                'e_time|结束时间' => 'require|date',
            ]);
            if (!$validate->check($post)) {
                $this->error($validate->getError());
            }
            $post['username'] = '1' . mt_rand(100000, 999900) . mt_rand(1000, 9999);
            $mCode = array('<', '>', '!', '@', '#', '{', '}', '~', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
            $post['password'] = '';
            foreach (array_rand($mCode, 6) as $key) {
                $post['password'] .= $mCode[$key];
            }
            $post['readmin'] = 2;
            $post['s_time'] = strtotime($post['s_time']);
            $post['e_time'] = strtotime($post['e_time']);
            $domain = db::name('product')->where('id', $post['product_id'])->value('domain');
            $wxpay = new Wxpay();
            $post['domain'] = $wxpay->domain($domain);
            $post['uid']=config('param.sys_uid');
            if (DB::name('product_run')->strict(false)->insert($post)) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        } else {
            $product = DB::name('product')->select();
            $this->assign('product', $product);
            $product_status = config('param.product_run_status');
            $this->assign('product_status', $product_status);
            return view();
        }
    }

    /*管理产品*/
    public function add()
    {
        $post = $this->request->param();
        if ($this->request->isPost()) {

            $info = DB::name('product_run')->where('diy_domain', $post['diy_domain'])->find();
            if ($info && $info['id'] != $post['id'] && !empty($post['diy_domain'])) {
                $this->error('自定义域名已经使用，请更换其他域名');
            }
            $data['e_time'] = strtotime($post['e_time']);
            $data['diy_domain']=$post['diy_domain'];
            $data['info'] = $post['info'];
            $data['status'] = $post['status'];
            $data['sys_info'] = $post['sys_info'];
            if (DB::name('product_run')->where('id', $post['id'])->update($data)) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        } else {
            if (isset($post['id']) && !empty($post['id'])) {
                $info = DB::name('product_run')->where('id', $post['id'])->find();
                //dump($info);
                $info['product_title'] = DB::name('product')->where('id', $info['product_id'])->value('title');
                $info['e_time'] = date('Y-m-d', $info['e_time']);
                $info['s_time'] = date('Y-m-d', $info['s_time']);
                $this->assign('info', $info);

            }
            $product_status = config('param.product_run_status');
            $this->assign('product_status', $product_status);
            return view();
        }
    }

    /*产品列表*/
    public function outorderlists()
    {
        $map = array();
        $data = array();
        $post = $this->request->param();
        if (isset($post['product_run_id']) && !empty($post['product_run_id'])) {
            $map[] = ['product_run_id', '=', $post['product_run_id']];
        }
        if (isset($post['uid']) && !empty($post['uid'])) {
            $map[] = ['uid', '=', $post['uid']];
        }
        $data['count'] = Db::view('product', ['id' => 'product_id', 'title'])
            ->view('product_order', 'id,product_id,c_time,uid,price,status,day,order_id', 'product.id=product_order.product_id')
            ->view('product_info', ['title' => 'info_title'], 'product_order.product_info_id=product_info.id')
            ->where($map)
            ->count();
        $data['data'] = Db::view('product', ['id' => 'product_id', 'title'])
            ->view('product_order', 'id,product_id,c_time,uid,price,status,day,order_id', 'product.id=product_order.product_id')
            ->view('product_info', ['title' => 'info_title'], 'product_order.product_info_id=product_info.id')
            ->where($map)
            ->order(['product_order.id' => 'desc'])
            ->page(input("param.page"), input('param.limit'))->select();
        $status = config('param.order_status');
        foreach ($data['data'] as &$item) {
            $item['status'] = $status[$item['status']];
            $item['c_time'] = date('Y-m-d H:i:s', $item['c_time']);
            $item['title'] = $item['title'] . '|' . $item['info_title'];
        }
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }

    /*产品列表*/
    public function readmin()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            $data['username'] = '1' . mt_rand(100000, 999900) . mt_rand(1000, 9999);
            $mCode = array('<', '>', '!', '@', '#', '{', '}', '~', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
            $data['password'] = '';
            foreach (array_rand($mCode, 6) as $key) {
                $data['password'] .= $mCode[$key];
            }
            $data['readmin'] = 2;
            if (DB::name('product_run')->where('id', $post['id'])->update($data)) {
                $this->success('重设操作成功，请10分钟后使用新用户名和密码登录!', '', json_encode($data));
            }
            $this->error('操作失败');
        }
    }


    public function lists()
    {
        $map = array();
        $data = array();
        $post = $this->request->param();
        if (isset($post['uid']) && !empty($post['uid'])) {
            $map[] = ['uid', '=', $post['uid']];
        }
        $data['count'] = DB::view('product_run', 'id,product_id,s_time,e_time,uid,domain,status,info,username,diy_domain,password,readmin')
            ->view('user', 'company', 'product_run.uid=user.id')
            ->view('product', 'title', 'product_run.product_id=product.id')
            ->where($map)
            ->count();
        $data['data'] = DB::view('product_run', 'id,product_id,s_time,e_time,uid,domain,status,info,username,diy_domain,password,readmin,sys_info')
            ->view('user', 'company', 'product_run.uid=user.id')
            ->view('product', 'title', 'product_run.product_id=product.id')
            ->where($map)
            // ->fetchSql(true)
            ->order('product_run.id desc')->page(input("param.page"), input('param.limit'))->select();
        $product_status = config('param.product_run_status');

        foreach ($data['data'] as &$info) {
            $info['status'] = $product_status[$info['status']];
            $info['s_time'] = date('Y-m-d', $info['s_time']);
            $info['e_time'] = date('Y-m-d', $info['e_time']);
        }
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }


}